package com.example.ywj.toutiao.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.ywj.toutiao.DetailActivity;
import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.Item;
import com.example.ywj.toutiao.bean.User;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MyAttentionBaseAdapter extends BaseAdapter {
    private static final String TAG = "MyAttentionBaseAdapter";
    private List<User> list;
    public Boolean flag = false;
    //String username;
    public MyAttentionBaseAdapter(List<User> list){
        this.list = list;
        //this.username = SpUtils.getString(this,"username");
    }
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //int currentType = getItemViewType(position);
        ViewHolder viewHolder ;
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.attention_item,null);
            viewHolder = new ViewHolder();
            viewHolder.avatar = convertView.findViewById(R.id.attention1_avatar);
            viewHolder.nickname = convertView.findViewById(R.id.attention1_nickname);
            viewHolder.desc = convertView.findViewById(R.id.attention1_desc);
            viewHolder.btn = convertView.findViewById(R.id.attention1_btn);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.nickname.setText(list.get(position).getNickname());
        viewHolder.desc.setText(list.get(position).getDesc());
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
        String url = Constant.BASEURL+"public/images/"+list.get(position).getAvatar()+".jpg";
        Glide.with(parent.getContext()).load(url).apply(requestOptions).into(viewHolder.avatar);
        viewHolder.btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(!flag){
                    cancelAttention(v.getContext(),list.get(position).getUsername());
                    viewHolder.btn.setBackground(v.getResources().getDrawable(R.drawable.buuton));
                    viewHolder.btn.setTextColor(v.getResources().getColor(R.color.white));
                    viewHolder.btn.setText("关注");
                    flag = true;
                }else{
                    attention(v.getContext(),list.get(position).getUsername());
                    viewHolder.btn.setBackground(v.getResources().getDrawable(R.drawable.buuton2));
                    viewHolder.btn.setTextColor(v.getResources().getColor(R.color.trgray));
                    viewHolder.btn.setText("已关注");
                    flag = false;
                }
            }
        });
        return convertView;
    }
    class ViewHolder{
        ImageView avatar;
        TextView nickname;
        TextView desc;
         Button btn;
    }
    public void attention(Context context,String attentionUser){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Constant.BASEURL)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        String username = SpUtils.getString(context,"username");
        Call<ResponseBody> call = responseApi.attention(attentionUser,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200&&obj.has("data")){
                    Toasty.success(context,"关注成功",Toasty.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void cancelAttention(Context context,String attentionUser){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Constant.BASEURL)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        String username = SpUtils.getString(context,"username");
        Call<ResponseBody> call = responseApi.cancelAttention(attentionUser,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200&&obj.has("data")){
                    Toasty.success(context,"取消关注",Toasty.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
}
